package com.yanqu.road.server.manager.activity.hsisland.handler.impl;

import com.yanqu.road.entity.activity.hsisland.HsIslandExploreResult;
import com.yanqu.road.entity.activity.hsisland.HsIslandUnionLog;
import com.yanqu.road.entity.activity.hsisland.config.HsIslandConfig;
import com.yanqu.road.entity.activity.hsisland.data.HsIslandBoxData;
import com.yanqu.road.entity.activity.hsisland.data.HsIslandEventData;
import com.yanqu.road.entity.activity.hsisland.data.HsIslandUnionData;
import com.yanqu.road.entity.activity.hsisland.enums.HsIslandEventIdEnum;
import com.yanqu.road.entity.activity.hsisland.enums.HsIslandLatticeTypeEnum;
import com.yanqu.road.entity.activity.hsisland.enums.HsIslandUnionLogTypeEnum;
import com.yanqu.road.entity.activity.hsisland.enums.HsIslandYnFlagEnum;
import com.yanqu.road.server.manager.activity.hsisland.*;
import com.yanqu.road.server.manager.activity.hsisland.handler.param.HsIslandExploreHandlerReq;
import com.yanqu.road.server.manager.activity.hsisland.handler.param.HsIslandExploreHandlerResp;
import com.yanqu.road.server.manager.activity.hsisland.handler.HsIslandIEventHandler;

import java.util.ArrayList;
import java.util.List;

public class HsIslandEventBoxHandler implements HsIslandIEventHandler {

    /**
     * 探索后处理
     * @param req
     * @return
     */
    @Override
    public HsIslandExploreResult explore(HsIslandExploreHandlerReq req) {
        CrossHsIslandActivity activity = req.getActivity();
        CrossHsIslandFloor floor = req.getFloor();
        CrossHsIslandIsland island = req.getIsland();
        CrossHsIslandUser user = req.getUser();
        CrossHsIslandUnion union = req.getUnion();
        HsIslandEventData eventData = req.getEventData();
        //注入宝箱发现人
        floor.setBoxFindUser(user.getUserData().getUserId());
        //生成宝箱事件
        List<HsIslandEventData> eventDataList = new ArrayList<>();
        for (HsIslandBoxData boxData :floor.getFloorData().getBoxDataMap().values()) {
            HsIslandEventData boxEventData = new HsIslandEventData(
                    activity.getActivityId(),
                    activity.getGroupId(),
                    user.getUserData().getUnionId(),
                    floor.getFloorData().getFloorId(),
                    island.getIslandData().getIslandId(),
                    user.getUserData().getUserId(),
                    floor.getUnEventIdAndIncrement(),
                    HsIslandEventIdEnum.EVENT_BOX.getType(),
                    eventData.getLightFlag()
            );
            boxEventData.setBoxId(boxData.getBoxId());
            boxEventData.addPosition(boxData.getX(), boxData.getY());
            eventDataList.add(boxEventData);
            island.putEvent(boxEventData);
        }
        //商会日志-有人发现海域宝库
        HsIslandUnionLog unionLog = new HsIslandUnionLog();
        unionLog.setType(HsIslandUnionLogTypeEnum.UNION_LOG_TYPE_3.getType());
        unionLog.setUserId(user.getUserData().getUserId());
        unionLog.setFloor(floor.getFloorData().getFloorId());
        unionLog.setTime(System.currentTimeMillis());
        union.addUnionLog(unionLog);
        //返回
        HsIslandExploreResult resp = new HsIslandExploreResult();
        resp.setEventDataList(eventDataList);
        return resp;
    }
}
